package www.yingdi365.com.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;

import java.util.HashMap;

import okhttp3.Call;
import okhttp3.Response;
import www.yingdi365.com.R;
import www.yingdi365.com.base.ZjbBaseActivity;
import www.yingdi365.com.constant.Constant;
import www.yingdi365.com.model.OkObject;
import www.yingdi365.com.model.NormalBack;
import www.yingdi365.com.util.GsonUtils;
import www.yingdi365.com.util.IsMobileNOorPassword;
import www.yingdi365.com.util.LogUtil;

public class WangJiMMActivity extends ZjbBaseActivity implements View.OnClickListener {

    private EditText editPhone;
    private EditText editYanZhengMa;
    private EditText editMiMA01;
    private EditText editMiMA02;
    private TextView textYanZhengMa;
    private Runnable mR;
    private int[] mI;
    private String mPhone_sms;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wang_ji_mm);
        init();
    }

    @Override
    protected void initSP() {

    }

    @Override
    protected void initIntent() {

    }

    @Override
    protected void findID() {
        editPhone = (EditText) findViewById(R.id.editPhone);
        editYanZhengMa = (EditText) findViewById(R.id.editYanZhengMa);
        editMiMA01 = (EditText) findViewById(R.id.editMiMA01);
        editMiMA02 = (EditText) findViewById(R.id.editMiMA02);
        textYanZhengMa = (TextView) findViewById(R.id.textYanZhengMa);
    }

    @Override
    protected void initViews() {
        ((TextView) findViewById(R.id.textViewTitle)).setText("忘记密码");
    }

    @Override
    protected void setListeners() {
        findViewById(R.id.imageBack).setOnClickListener(this);
        findViewById(R.id.buttonTiJiao).setOnClickListener(this);
        textYanZhengMa.setOnClickListener(this);
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.textYanZhengMa:
                sendSMS();
                break;
            case R.id.buttonTiJiao:
                if (TextUtils.isEmpty(mPhone_sms)) {
                    Toast.makeText(WangJiMMActivity.this, "请先发送验证码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(editYanZhengMa.getText().toString().trim())) {
                    Toast.makeText(WangJiMMActivity.this, "请填写验证码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (TextUtils.isEmpty(editMiMA01.getText().toString().trim()) || TextUtils.isEmpty(editMiMA02.getText().toString().trim())) {
                    Toast.makeText(WangJiMMActivity.this, "请填写密码", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (!TextUtils.equals(editMiMA01.getText().toString().trim(), editMiMA02.getText().toString().trim())) {
                    Toast.makeText(WangJiMMActivity.this, "两次密码不一致", Toast.LENGTH_SHORT).show();
                    return;
                }

                showLoadingDialog();
                OkObject okObject = getOkObject();
                OkGo.post(okObject.getUrl())//
                        .tag(this)//
                        .upJson(okObject.getJson())//
                        .execute(new StringCallback() {
                            @Override
                            public void onSuccess(String s, Call call, Response response) {
                                cancelLoadingDialog();
                                LogUtil.LogShitou("WangJiMMActivity--onSuccess", "修改密码" + s);
                                try {
                                    NormalBack normalBack = GsonUtils.parseJSON(s, NormalBack.class);
                                    if (normalBack.isSuccess()) {
                                        finish();
                                    }
                                    Toast.makeText(WangJiMMActivity.this, normalBack.getMessage(), Toast.LENGTH_SHORT).show();
                                } catch (Exception e) {
                                    Toast.makeText(WangJiMMActivity.this, getResources().getString(R.string.shujuchucuo), Toast.LENGTH_SHORT).show();
                                }
                            }

                            @Override
                            public void onError(Call call, Response response, Exception e) {
                                super.onError(call, response, e);
                                cancelLoadingDialog();
                                Toast.makeText(WangJiMMActivity.this, getResources().getString(R.string.qingqiushibai), Toast.LENGTH_SHORT).show();
                            }
                        });
                break;
            case R.id.imageBack:
                finish();
                break;
        }
    }

    private OkObject getOkObject() {
        String url = Constant.HOST + Constant.Url.LOGINSERVICE + "password/modify";
        HashMap<String, String> params = new HashMap<>();
        params.put("mobile", mPhone_sms);
        params.put("newPassword", editMiMA01.getText().toString());
        params.put("validCode", editYanZhengMa.getText().toString());
        params.put("verifyType", "code");
        return new OkObject(null,params, url);
    }

    /**
     * des： 发送短信
     * author： ZhangJieBo
     * date： 2017/7/6 0006 下午 2:45
     */
    private void sendSMS() {
        textYanZhengMa.removeCallbacks(mR);
        boolean mobileNO = IsMobileNOorPassword.isMobileNO(editPhone.getText().toString().trim());
        if (mobileNO) {
            mPhone_sms = editPhone.getText().toString().trim();
            textYanZhengMa.setEnabled(false);
            mI = new int[]{60};

            mR = new Runnable() {
                @Override
                public void run() {
                    textYanZhengMa.setText((mI[0]--) + "秒后重发");
                    if (mI[0] == 0) {
                        textYanZhengMa.setEnabled(true);
                        textYanZhengMa.setText("重新发送");
                        return;
                    } else {
                    }
                    textYanZhengMa.postDelayed(mR, 1000);
                }
            };
            textYanZhengMa.postDelayed(mR, 0);
            getSms();
        } else {
            Toast.makeText(WangJiMMActivity.this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
            editPhone.setText("");
        }
    }

    /**
     * des： 获取短信
     * author： ZhangJieBo
     * date： 2017/7/6 0006 下午 2:45
     */
    private void getSms() {
        showLoadingDialog();
        OkGo.get(Constant.HOST + Constant.Url.LOGINSERVICE + "password/validcode/" + mPhone_sms)//
                .tag(this)//
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        LogUtil.LogShitou("WangJiMMActivity--onSuccess", "发送短信" + s);
                        cancelLoadingDialog();
                        NormalBack normalBack = GsonUtils.parseJSON(s, NormalBack.class);
                        if (!normalBack.isSuccess()) {
                            Toast.makeText(WangJiMMActivity.this, normalBack.getMessage(), Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(WangJiMMActivity.this, "短信已发送", Toast.LENGTH_SHORT).show();
                        }
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                        cancelLoadingDialog();
                        Toast.makeText(WangJiMMActivity.this, getResources().getString(R.string.qingqiushibai), Toast.LENGTH_SHORT).show();
                    }
                });
    }

}
